package dao.hibernate;

import org.hibernate.Session;

import users.Subscription;

import dao.SubscriptionDAO;

/**
 * Implementation of a DAO to allow modifications of a subscription in a
 * MySQL database using Hibernate.
 */
public class SubscriptionDAOHibernateImpl implements SubscriptionDAO {

    /**
     * Save the given subscription in the database.
     *
     * @param sub  subscription to save in the database
     */
    @Override
    public void saveSubscription(final Subscription sub) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.save(sub);

        session.getTransaction().commit();
    }

    /**
     * Remove the given subscription from the database.
     *
     * @param sub  subscription to remove from the database
     */
    @Override
    public void removeSubscription(final Subscription sub) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.delete(sub);

        session.getTransaction().commit();
    }

}
